package com.sec.controller;

import com.sec.dao.mysql.SIntroDao;
import com.sec.domain.mysql.SIntro;
import com.sec.util.ConstantUtils;
import com.sec.util.FileUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 介绍信息controller
 * Created by Administrator on 2017/12/3.
 */
@Controller
@RequestMapping("/introManager")
public class IntroController {

    @Resource
    private SIntroDao sIntroDao;

    // 介绍list页
    @RequestMapping(value = "/list")
    public String index() {
        return "/intro/list.html";
    }

    // 详情页面
    @RequestMapping(value = "/detail")
    public String detail() {
        return "/intro/detail.html";
    }

    // 获取list
    @RequestMapping(value = "/getList",produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String getList(){
        List<SIntro> sIntroList = sIntroDao.queryAll();
        JSONArray jsonData = JSONArray.fromObject(sIntroList);
        return jsonData.toString();
    }

    // 获取详情
    @RequestMapping(value = "/getDetail",produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String getDetail(int i_id){
        SIntro intro = sIntroDao.queryById(i_id);
        JSONObject intro_json = JSONObject.fromObject(intro);
        return intro_json.toString();
    }

    // 更新
    @RequestMapping(value = "/update")
    @ResponseBody
    public String update(int i_id, String i_name, String i_intro, HttpServletRequest request) throws IOException {
        SIntro intro = sIntroDao.queryById(i_id);
        intro.setI_name(i_name);
        intro.setI_intro(i_intro);
        sIntroDao.update(intro);

        // 复制图片文件夹到指定目录下
        String srcPath = request.getSession().getServletContext().getRealPath("/ueditor");
        ConstantUtils constantUtils = new ConstantUtils();
        File src  = new File(srcPath);
        if(src.exists()){
            FileUtil.copyFolder(srcPath, constantUtils.getValue("xjtaxPath") + "/ueditor");
        }
        return "success";
    }
}
